//
//  DBManager.m
//  BobNews
//
//  Created by lanou3g on 16/5/11.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "DBManager.h"
#import "FMDB.h"
#import "UserInfoManager.h"
#import "ToutiaoModel+HTML.h"
#import "BaseModel.h"
@implementation DBManager

/// 单例
+ (instancetype)shareManager {
    static DBManager *manager = nil;
    @synchronized(self) {
        if (!manager) {
            manager = [[DBManager alloc]init];
        }
    }
    return manager;
}

/// 静态变量：数据库指针
static FMDatabase *database = nil;
/// 打开数据库
- (void)openDB {
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    /// 数据库路径
    NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"artical.sqlite"];
    NSLog(@"%@",dbPath);
    
    /// 打开、创建数据库
    database = [FMDatabase databaseWithPath:dbPath];
    if ([database open]) {
        NSLog(@"打开数据库");
    }
}

/// 创建表
- (void)createTable {
    NSString *sql = @"CREATE TABLE IF NOT EXISTS artical(collecid INTEGER PRIMARY KEY AUTOINCREMENT,uid TEXT,docid TEXT,title TEXT,imgsrc TEXT,source TEXT,replyCount TEXT,html TEXT)";
    BOOL result = [database executeUpdate:sql];
    if (result) {
        NSLog(@"创建表成功");
    }
    
}

/// 插入数据
- (void)insertModel:(ToutiaoModel *)model html:(NSString *)htmlStr {
    BOOL result = [database executeUpdateWithFormat:@"INSERT INTO artical(uid,docid,title,imgsrc,source,replyCount,html) VALUES(%@,%@,%@,%@,%@,%@,%@)",[UserInfoManager shareManager].uid,model.docid,model.title,model.imgsrc,model.source,model.replyCount,htmlStr];
    if (result) {
        NSLog(@"插入数据成功");
    }
}

/// 收藏
- (void)collectionModel:(ToutiaoModel *)model html:(NSString *)htmlStr {
    [self openDB];
    [self createTable];
    [self insertModel:model html:htmlStr];
}

/// 取消收藏
- (void)cancelCollectionArtical:(NSString *)docid {
    [self openDB];
    [self createTable];
    BOOL result = [database executeUpdateWithFormat:@"DELETE FROM artical WHERE docid = %@",docid];
    if (result) {
        NSLog(@"删除成功");
    }
}

/// 查询所有收藏的内容
- (NSArray *)selectAllCollection {
    [self openDB];
    [self createTable];
    FMResultSet *resultSet = [database executeQueryWithFormat:@"SELECT * FROM artical WHERE uid = %@",[UserInfoManager shareManager].uid];
    
    NSMutableArray *array = [NSMutableArray array];
    while ([resultSet next]) {
        ToutiaoModel *model = [[ToutiaoModel alloc]init];
        model.title = [resultSet stringForColumn:@"title"];
        model.imgsrc = [resultSet stringForColumn:@"imgsrc"];
        model.docid = [resultSet stringForColumn:@"docid"];
        model.source = [resultSet stringForColumn:@"source"];
        model.replyCount = (NSNumber *)[resultSet stringForColumn:@"replyCount"];
        model.htmlStr = [resultSet stringForColumn:@"html"];
        
        [array addObject:model];
    }
    return array;
}

/// 判断是否收藏过
- (BOOL)hasCollectionDocid:(NSString *)docid {
    [self openDB];
    [self createTable];
    
    BOOL hasCollect = NO;
    
    FMResultSet *result = [database executeQueryWithFormat:@"SELECT * FROM artical WHERE uid = %@ AND docid = %@",[UserInfoManager shareManager].uid,docid];
    while ([result next]) {
        hasCollect = YES;
    }
    return hasCollect;
}

@end
